<?php
/**
 * class App_Plugin_Common
 *
 * common hooks
 *
 */
class App_Plugin_Common extends Zend_Controller_Plugin_Abstract
{
    /**
     * hook for route shutdown
     * most of the things can be done within this hook
     *
     * @param Zend_Controller_Request_Abstract
     * @return null
     */
    public function routeShutdown(Zend_Controller_Request_Abstract $request)
    {
        // get bootstrap object
        $bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');

        // get view object
        $bootstrap->bootstrap('View');
        $view = $bootstrap->getResource('View');

        // common url for view scripts
        $view->commonUrl = $view->baseUrl();

        /**
         * check whether current user is loged in
         * store current visiter as user object for global usage
         */
        if (App_Model_User::isLogedIn()) {
            $email = Zend_Auth::getInstance()->getIdentity();
            $user = new App_Model_User();
            Zend_Registry::set('user', $user->getProfile($email));
            $view->userLogedIn = true;
        } else {
            $view->userLogedIn = false;
            $view->formLogin = new App_Form_User_Login();
        }

        // application.ini configurations
        //$config = $bootstrap->getOptions();

        /**
         * database caching
         *
         * @todo switch to memcache or apc if necessary
         * @todo move cache->clean to admin cache management module
         */
        /*
        if (is_dir($config['resources']['db']['cachepath']['metadata'])) {
            $frontendOptions = array('automatic_serialization' => true);
            $backendOptions  = array('cache_dir' => $config['resources']['db']['cachepath']['metadata']);
            $cache = Zend_Cache::factory('Core', 'File', $frontendOptions, $backendOptions);
            //$cache->clean(Zend_Cache::CLEANING_MODE_OLD);
            Zend_Db_Table::setDefaultMetadataCache($cache);
        }

        if (is_dir($config['resources']['db']['cachepath']['tabledata'])) {
            $frontendOptions = array(
                'caching'=> true,
                'cache_id_prefix' => null,
                'lifetime' => 7200,
                'logging' => false,
                'write_control'=> true,
                'automatic_serialization' => true,
                'automatic_cleaning_factor' => 10,
                'ignore_user_abort'=> false,
            );
            $backendOptions  = array('cache_dir' => $config['resources']['db']['cachepath']['tabledata']);
            $tableCache = Zend_Cache::factory('Core', 'File', $frontendOptions, $backendOptions);
            Zend_Registry::set('table_cache', $tableCache);
        }
        */
    }

}